#include "menu_item.h"

extern int G_W, G_H;

MENU_ITEM::MENU_ITEM(MENU_ITEM_TYPE type, QTimeLine* tl_anims, CASH *cash):QObject(), QGraphicsItemGroup()
{

    this->cash=cash;

    this->type = type;
    this->tl_anims = tl_anims;
    hover_pos_factor = 50;

    init();
    anim_init();

    if(type != MAIN){
        item_hover->setOpacity(0);
    }

    this->setPos(pos_unvis);
}

QPainterPath MENU_ITEM::shape() const
{
    return QPainterPath();
}

QRectF MENU_ITEM::boundingRect() const
{
    return QRectF();
}

void MENU_ITEM::init()
{
    anim = new QGraphicsItemAnimation(this);
    tl_hover = new QTimeLine(300, this);

    switch (type) {
    case STORY:
        item_normal = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "story.png"), this);
        item_hover = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "story_hover.png"), this);

        pos_normal = QPointF(G_W - 417, G_H - 460);
        pos_unvis = QPointF(G_W, G_H - 460);
        pos_hover = QPointF(G_W - 417 - hover_pos_factor, G_H - 460);

        anim->setPosAt(0.6, pos_normal);

        break;

    case BRUTAL:
        item_normal = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "brutal.png"), this);
        item_hover = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "brutal_hover.png"), this);

        pos_normal = QPointF(G_W - 417, G_H - 369);
        pos_unvis = QPointF(G_W, G_H - 369);
        pos_hover = QPointF(G_W - 417 - hover_pos_factor, G_H - 369);

        anim->setPosAt(0.4, pos_normal);

        break;

    case ENEMIES:
        item_normal = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "enemies.png"), this);
        item_hover = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "enemies_hover.png"), this);

        pos_normal = QPointF(G_W - 417, G_H - 280);
        pos_unvis = QPointF(G_W, G_H - 280);
        pos_hover = QPointF(G_W - 417 - hover_pos_factor, G_H - 280);

        anim->setPosAt(0.2, pos_normal);

        break;

    case SETTINGS:
        item_normal = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "settings.png"), this);
        item_hover = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "settings_hover.png"), this);

        pos_normal = QPointF(G_W - 417, G_H - 183);
        pos_unvis = QPointF(G_W, G_H - 183);
        pos_hover = QPointF(G_W - 417 - hover_pos_factor, G_H - 183);

        anim->setPosAt(0.4, pos_normal);

        break;

    case QUIT:
        item_normal = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "quit.png"), this);
        item_hover = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "quit_hover.png"), this);

//        pos_normal = QPointF(G_W - 417, G_H - 95);
//        pos_unvis = QPointF(G_W, G_H - 95);
//        pos_hover = QPointF(G_W - 417 - hover_pos_factor, G_H - 95);

        pos_normal = QPointF(G_W - 417, G_H - 280);
        pos_unvis = QPointF(G_W, G_H - 280);
        pos_hover = QPointF(G_W - 417 - hover_pos_factor, G_H - 280);

        anim->setPosAt(0.6, pos_normal);

        break;

    case MAIN:
        item_normal = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "main/main.png"), this);
        item_hover = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_MENU_PATH + "main/main_blue.png"), this);

        pos_normal = QPointF(16, 5);
        pos_unvis = QPointF(16, - item_normal->boundingRect().height());

        anim->setPosAt(1, pos_normal);
        break;

    default:
        qDebug() << "ERROR !!! type not defined!! menu items";
        break;
    }

    if(item_normal->pixmap().isNull() || item_hover->pixmap().isNull()){
        qDebug() << "ERROR !!! MENU_ITEMS TEXTURES NOT LOADED!!!!";
        QApplication::closeAllWindows();
    }

    this->addToGroup(item_normal);
    this->addToGroup(item_hover);

}

void MENU_ITEM::anim_init()
{
    anim->setTimeLine(tl_anims);
    anim->setItem(this);

    anim->setPosAt(0, pos_unvis);

    anim_hover = new QGraphicsItemAnimation();
    anim_hover->setTimeLine(tl_hover);
    anim_hover->setItem(this);

    anim_hover->setPosAt(0, pos_normal);
    anim_hover->setPosAt(1, pos_hover);

    prop_anim = new QPropertyAnimation(this, "transparent", this);
    prop_anim->setDuration(1000);

    if(type == MAIN){
        prop_anim->setLoopCount(-1);
        prop_anim->setDuration(3000);

        prop_anim->setKeyValueAt(0, 0.5);
        prop_anim->setKeyValueAt(0.5, 1);
        prop_anim->setKeyValueAt(1, 0.5);

        prop_anim->start();
    }
}

void MENU_ITEM::set_focus(bool value)
{
    if(value){
        tl_hover->stop();
        tl_hover->setDirection(QTimeLine::Forward);
        tl_hover->start();

        prop_anim->stop();
        prop_anim->setLoopCount(-1);
        prop_anim->setKeyValueAt(0, 0);
        prop_anim->setKeyValueAt(0.5, 1);
        prop_anim->setKeyValueAt(1, 0);

        prop_anim->start();
    }else{
        tl_hover->stop();
        tl_hover->setDirection(QTimeLine::Backward);
        tl_hover->start();

        prop_anim->stop();
        prop_anim->setLoopCount(1);

        prop_anim->setKeyValueAt(0, prop_anim->currentValue());

        prop_anim->setKeyValueAt(1, 0);

        prop_anim->start();
    }
}

//Property
qreal MENU_ITEM::Transparent()
{
    return item_hover->opacity();
}

void MENU_ITEM::setTransparent(qreal value)
{
    item_hover->setOpacity(value);
}


QTimeLine *MENU_ITEM::Get_tl_hover()
{
    return this->tl_hover;
}

MENU_ITEM_TYPE MENU_ITEM::Get_item_type()
{
    return this->type;
}

MENU_ITEM::~MENU_ITEM()
{
}
